﻿using System.Web.Mvc;
using TSharp.Core.SystemOption.Facade;
using TSharp.Core.Util;
using TSharp.Core.SystemOption.Store.Entities;


namespace TSharp.Core.SystemOption.Impl
{
    public class AModelBinder : IModelBinder, IAppHandler
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var t = bindingContext.Model as Order ?? new Order();
            t.ShipName = GetValue(bindingContext, "ShipName").ToStringObject();
            return t;
        }

        public void OnStart(AppEventArgs args)
        {
            ModelBinders.Binders.Add(typeof(Order), new AModelBinder());

          
        }
         
        private static object GetValue(ModelBindingContext bindingContext, string key)
        {
            ValueProviderResult valueResult = bindingContext.ValueProvider.GetValue(key);
            bindingContext.ModelState.SetModelValue(key, valueResult);
            return valueResult != null ? valueResult.RawValue : null;
        }


        public void OnStop(AppEventArgs args)
        { 
        }
    }
}